package com.imy.lua.view.console;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.imy.common.base.BaseCustomViewGroup;
import com.imy.common.root.RootTools;
import com.imy.common.util.CPUUtils;
import com.imy.common.util.UiUtil;
import com.imy.lua.R;
import com.imy.touch.TouchCore;
import com.imy.touch.root.client.SocketClient;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ConsoleView extends BaseCustomViewGroup {
    private List<String> mList;
    private ListView mListView;
    private boolean open = false;
    private ConsoleAdapter adapter;

    public ConsoleView(Context context) {
        super(context);
    }

    public ConsoleView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ConsoleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(View inflate) {
        mListView = inflate.findViewById(R.id.list_view);
        inflate.findViewById(R.id.close_iv).setOnClickListener(v -> hide());
        inflate.findViewById(R.id.delete_iv).setOnClickListener(v -> {
            adapter.getLists().clear();
            adapter.notifyDataSetChanged();
            mList.clear();
        });
        int deviceHeight = UiUtil.getDeviceHeight(getContext());
        int deviceWidth = UiUtil.getDeviceWidth(getContext());

        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(deviceWidth, deviceHeight);
        setLayoutParams(layoutParams);
        mList = new ArrayList<>();
        String[] supportCpu = CPUUtils.getSupportCpu();
        mList.add("本机支持架构：" + Arrays.toString(supportCpu));
        mList.add("本机Root:" + RootTools.isRootAvailable());
        SocketClient client = TouchCore.get().getClient();
        if (client == null) {
          mList.add("是否支持root点击:"+false  );
        }else {
          mList.add("是否支持root点击:"+true  );
        }
        mList.add("------------脚本信息------------");
        adapter = new ConsoleAdapter(getContext(), mList);
        mListView.setAdapter(adapter);
    }

    public void addLog(String s) {
        mList.add(s);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.custom_view_console;
    }

    public void openOrClose() {
        if (open) {
            setVisibility(GONE);
            open = false;
        } else {
            setVisibility(VISIBLE);
            adapter.setLists(mList);
            adapter.notifyDataSetChanged();
            open = true;
        }
    }

    public void hide() {
        setVisibility(GONE);
        open = false;
    }
}
